package org.superwindcloud.beauti_store.services;

import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.superwindcloud.beauti_store.dao.GirlPhoto;
import org.superwindcloud.beauti_store.repository.GirlPhotoRepository;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Pattern;

@Service
@Data
@RequiredArgsConstructor
public class GirlPhotoService {

    private final GirlPhotoRepository repository;

    public final void saveAllGirlImages() throws IOException {
        Path assetPath = new ClassPathResource("assets").getFile().toPath();
        Pattern pattern = Pattern.compile("girl.*\\.jpg", Pattern.CASE_INSENSITIVE);

        Files.walk(assetPath)
                .filter(Files::isRegularFile)
                .filter(path -> pattern.matcher(path.getFileName().toString()).matches())
                .forEach(path -> {
                    try {
                        GirlPhoto photo = new GirlPhoto();
                        photo.setName(path.getFileName().toString());
                        photo.setData(Files.readAllBytes(path));
                        photo.setContentType("image/jpeg");
                        repository.save(photo);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
    }

}